/**
 * Copyright (c) 2011-2012
 * SETeam11
 */

package de.tum.in.oose.bumpers.cars.instruments;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * This is the AnalogSpeedmeter - responsible for displaying current speed.
 * 
 */
public class AnalogSpeedometer extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private int angle;
	
	/**
	 * Standard Constructor for analog speedometer.
	 * Sets standard values for angle (0) and the background color (white)	
	 */
	public AnalogSpeedometer(){
		super(true);
		angle = 0;
		setBackground(Color.white);
	}
	/**
	 * Setter for the Angle of the speedometer.
	 * @param angle must be between min and max (0 - 180)
	 */
	public void setAngle(int angle){
		if(angle < getMinAngle()
		   || angle > getMaxAngle()){
			throw new IllegalArgumentException("Angle must be between minumum and maximum angle.");
		}
		this.angle = angle;
		repaint();
	}
	
	/**
	 * Returns the minimum possible angle: 0.
	 * @return minimum possible angle
	 */
	public int getMinAngle(){ return 0; }
	
	/**
	 * Returns the maximum possible angle: 180.
	 * @return maximum possible angle 
	 */
	public int getMaxAngle(){ return 180; }
	
	/**
	 * Paint method overriden for custom drawing.
	 * @param g the graphics to be drawn
	 */
	public void paint(Graphics g){
		super.paint(g);
		
		g.drawArc(0, getHeight()/2, getWidth(), 2*getHeight(),
				  360,180);
		
		double x = Math.cos(Math.toRadians(this.angle));
		x = -1*x;
		int width = getWidth()/2;
		int toX = (int)(width + width*x);
		
		double y = Math.sin(Math.toRadians(this.angle));
		int toY = (int)(getHeight()-2 - getHeight()*y);
		
		g.drawLine(getWidth()/2, getHeight()-2, toX, toY);
		
	}
	/**
	 * Returns the Dimension for the preferred size.
	 * @return Dimension with (100,100)
	 */
	public Dimension getPreferredSize(){
		return new Dimension(100,100);
	}
	
	/**
	 * Returns the maximum dimensions for the speedometer depending on the maximum size's height.
	 * @return maximum possible dimensions
	 */
	public Dimension getMaximumSize(){
		int height = super.getMaximumSize().height;
		return new Dimension(height,height);
	}
	
	/**
	 * Returns the minimum dimensions for the speedometer depending on the minimum size's height.
	 * @return minimum possible dimensions
	 */
	public Dimension getMinimumSize(){
		int height = super.getMinimumSize().height;
		return new Dimension(height,height);
	}
	
	/**
	 * Returns the current dimensions for the speedometer.
	 * @return current dimensions
	 */
	public Dimension getSize(){
		int height = super.getSize().height;
		return new Dimension(height, height);
	}
	
	
}

